"""
Case Type   : SQL语法
Case Name   : 分区表多次truncate后,再进行vacuum freeze pg_partition,查询系统表
              pg_partition索引
Create At   : 2023/02/18
Owner       : cr13
Description :
    1.创建分区表
    2.创建索引
    3.修改表分区
    4.重启数据库
    5.执行 vacuum freeze pg_partition;
    6.执行查询语句
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.返回查询结果
    7.成功
History     :
"""

import re
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.tbname = 't_partition_truncate_0326'
        self.index = 'i_partition_truncate_0326'

    def test_partition_table(self):
        text = "----step1:创建分区表 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tbname} cascade;" \
            f"create table {self.tbname}(a numeric, b float8) " \
            f"partition by range(a)" \
            f"(" \
            f"    partition p1 values less than (100)," \
            f"    partition p2 values less than (200)," \
            f"    partition p3 values less than (MAXVALUE));"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败' +
                      text)

        text = "----step2:创建索引 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop index if exists {self.index};create index " \
            f"{self.index} on {self.tbname}(b) global;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, result,
                      '执行失败' + text)

        text = "----step3:修改表分区 expect:成功----"
        self.log.info(text)
        sql_cmd = f"alter table {self.tbname} truncate partition for (50) " \
            f"update global index;alter table {self.tbname} truncate " \
            f"partition for (50);"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败' + text)

        text = "----step4:重启数据库 expect:成功----"
        self.log.info(text)
        result = self.commsh.restart_db_cluster()
        self.assertTrue(result, '执行失败' + text)

        text = "----step5:执行 vacuum freeze pg_partition expect:成功----"
        self.log.info(text)
        sql_cmd = "vacuum freeze pg_partition;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "----step6:执行查询语句 expect:返回查询结果----"
        self.log.info(text)
        sql_cmd = f"select /*+ tablescan(pg_partition) */ oid,relname," \
            f"reloptions,parentid,relfilenode from pg_partition where " \
            f"parttype = 'p';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        flg1 = re.search(r'.*p2.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg1, '执行失败' + text)
        flg2 = re.search(r'.*p3.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg2, '执行失败' + text)
        flg3 = re.search(r'.*p1.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg3, '执行失败' + text)
        sql_cmd = f"select /*+ indexscan(pg_partition pg_partition_" \
            f"partoid_index) */ oid,relname,reloptions,parentid,relfilenode " \
            f"from pg_partition where parttype = 'p';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        flg1 = re.search(r'.*p1.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg1, '执行失败' + text)
        flg2 = re.search(r'.*p2.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg2, '执行失败' + text)
        flg3 = re.search(r'.*p3.*{orientation=row,compression=no}.*', result,
                         re.S)
        self.assertTrue(flg3, '执行失败' + text)

    def tearDown(self):
        text = '-----step7:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table {self.tbname} cascade;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
